﻿using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using CSF.WinApp.JobNotifier.ServiceBLL;
namespace CSF.WinApp.JobNotifier
{
    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        ServiceBLL.JobNotifyServiceClient jobNotifyBll = new ServiceBLL.JobNotifyServiceClient();
        private string ticket = Convert.ToString(AppDomain.CurrentDomain.GetData("ticket"));
        //动态读取顺序
        private int no = 0;

        #region timDisplay_Tick
        /// <summary>
        /// 隔5秒钟自动执行一次
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timDisplay_Tick(object sender, EventArgs e)
        {
            ServiceBLL.JobNotifyMessage mess = jobNotifyBll.GetNextJob(ticket, no);
            lblTotal.Text = Convert.ToString(mess.TotalJobCount);
            lblMy.Text = Convert.ToString(mess.MyJobCount);
            lblJobNo.Text = mess.JobNo;
            lblItemCode.Text = mess.ItemCode;
            lblQuantity.Text =Convert.ToString(mess.ItemQty);
            no++;
            if (no >= mess.MyJobCount)
            {
                no = 0;
            }
           
            WorkProcessMessage[] proMess = jobNotifyBll.GetWorkProcess(ticket);
            int i = 0;
            foreach (WorkProcessMessage item in proMess)
            {
                ListViewItem lvItem = new ListViewItem();
                lvItem.Text = item.Name;
                int[] finishId= mess.FinishedProcessId;    
                PictureBox img = pnlImg.Controls[i] as PictureBox;
                bool x = false;
                foreach (int id in finishId)
                {
                    if (id == item.Id)
                    {
                        x = true;
                        break;
                    }
                }
               string s= img.Name;
                if (x)
                {
                    img.Image = imgList.Images[1];
                }
                else
                    img.Image = imgList.Images[0];
                Label lb = pnlLbl.Controls[i] as Label;
                lb.Text = item.Name;
                i++;
            }
        }
        #endregion

        #region Main_Load
        private void Main_Load(object sender, EventArgs e)
        {
            timDisplay_Tick(null, null);
            pnlDetails.Visible = false;
            this.Height = 180;
        }
        #endregion

        #region 回车执行动作
        private void txtJobNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                string Qulity = txtQty.Text.Trim();
                try
                {
                    if(!string.IsNullOrEmpty(Qulity))
                    {
                        int q = Convert.ToInt32(Qulity);
                    }
                }
                catch
                {
                    MessageBox.Show("Qulity must be a digatel!", "Tips", MessageBoxButtons.OK);
                    return;
                }
                string jobNo = txtJobNo.Text.Trim();
                string userId = ticket;
                string ItemCode = txtItemCode.Text.Trim();
                string GP1 = txtGP1.Text.Trim();
                bool IsUrgent = chbUrgent.Checked;
                string Comment = txtComment.Text.Trim();

                int result = jobNotifyBll.SetProcessJob(ticket, jobNo, ItemCode, Qulity, GP1, IsUrgent, Comment);

                if (result == 1)
                {
                    //判断一下,当前jobno是否全部完成
                    jobNotifyBll.FinishByJonNo(jobNo);
                    MessageBox.Show("Save success!", "Tips", MessageBoxButtons.OK);
                    txtJobNo.Text = "";
                    timDisplay_Tick(null, null);
                }
                if (result == 0)
                {
                    MessageBox.Show("Does not allow new jobno!","Tips",MessageBoxButtons.OK);
                }
                if (result == -1)
                {
                    MessageBox.Show("Save fail!", "Tips", MessageBoxButtons.OK);
                }
            }
        }
        #endregion

        #region btnDetails_Click
        private void btnDetails_Click(object sender, EventArgs e)
        {
            pnlDetails.Visible = !pnlDetails.Visible;
            if (pnlDetails.Visible)
            {
                btnDetails.Text = "up";
                this.Height += 170;
                txtItemCode.Focus();
            }
            else
            {
                this.Height -= 170;
                btnDetails.Text = "down";
            }

        }
        #endregion

        #region lblTotal_Click
        private void lblTotal_Click(object sender, EventArgs e)
        {
            Details det = new Details();
            det.Show();
        }
        #endregion

        #region 切换用户
        private void btnLogoff_Click(object sender, EventArgs e)
        {
            //切换用户
            if (MessageBox.Show("Are you logout?", "Tips", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                (new Common()).ClearUserInfo();
                this.Close();
                System.Diagnostics.Process.Start(System.Reflection.Assembly.GetExecutingAssembly().Location);
            }
        }
        #endregion

        #region lblMy_Click
        private void lblMy_Click(object sender, EventArgs e)
        {
            MyDetails det = new MyDetails();
            det.Text = "";
            det.Show();
        }
        #endregion

        #region 移动
        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);

        private const int WM_SYSCOMMAND = 0x0112;//点击窗口左上角那个图标时的系统信息  
        private const int WM_MOVING = 0x216;//鼠标移动消息  
        private const int SC_MOVE = 0xF010;//移动信息  
        private const int HTCAPTION = 0x0002;//表示鼠标在窗口标题栏时的系统信息  
        private const int WM_NCHITTEST = 0x84;//鼠标在窗体客户区（除了标题栏和边框以外的部分）时发送的消息  
        private const int HTCLIENT = 0x1;//表示鼠标在窗口客户区的系统消息  
        private const int SC_MAXIMIZE = 0xF030;//最大化信息  
        private const int SC_MINIMIZE = 0xF020;//最小化信息  

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WM_MOVING: //如果鼠标移                    
                    base.WndProc(ref m);//调用基类的窗口过程——WndProc方法处理这个消息  
                    if (m.Result == (IntPtr)HTCLIENT)//如果返回的是HTCLIENT  
                    {
                        m.Result = (IntPtr)HTCAPTION;//把它改为HTCAPTION  
                        return;//直接返回退出方法  
                    }
                    break;
            }
            base.WndProc(ref m);//如果不是鼠标移动或单击消息就调用基类的窗口过程进行处理  
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                ReleaseCapture();
                SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
            }
            base.OnMouseMove(e);
        }
        #endregion
    }
}
